/* Team Directories
 *
 * You are free to do whatever you want with this code. Frankly we don't give a damn.
 * 
 *  
 * Copyright GNU General Public License v3
 */

package StudyBuddy;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Ra
 */
import java.io.*;
public class ReadWrite {
        
    public ReadWrite()
    {
        
    }
//    public int CountLines(String file)
//    {
//        BufferedReader inputStream1 = null;
//        
//        try
//        {
//            inputStream1 = new BufferedReader(new FileReader(file));
//            line = inputStream1.readLine();
//           
//            while( line != null)
//            {
//                countLines++;
//            }// while
//            System.out.println("Counting " + countLines);
//            inputStream1.close();
//            
//        }
//        catch(FileNotFoundException e)
//        {
//            System.out.println("File " + file + "was not found.");
//        }
//        catch(IOException e)
//        {
//            System.out.println("Error reading from file " + file +".");
//        }
//        
//        return countLines;
//    }// count lines
    public Stack Read(String file)
    {
        Stack st = new Stack();
        String line = "blank";
        
        BufferedReader inputStream = null;
        
        try
        {
              file = file + ".txt";
             // System.out.println("file contains" + file);
            inputStream = new BufferedReader(new FileReader(file));
            
            line = inputStream.readLine();
            
           // storage = new String[countLines];
            while(line != null)
            {
               // System.out.println("Reading.. " + line);
                // storage[i] = line;
                // i++;
                st.push(line);
                line = inputStream.readLine();
            }
            inputStream.close();
        }
        catch(FileNotFoundException e)
        {
            System.out.println("File " + file + "was not found.");
        }
        catch(IOException e)
        {
            System.out.println("Error reading from file " + file +".");
        }
        
        return st;
    }//Read
    public void Write(String line, String file)
    {
       PrintWriter outputStream = null;
       try
       {
         
          
           outputStream = new PrintWriter(new FileOutputStream(file,true));
           System.out.println("Writting " + line);
       }
       catch(FileNotFoundException e)
       {
           System.out.println("Error opening the " + file + ".");
           System.exit(0);
       }
       outputStream.println(line);
       outputStream.close();
       
    }

}//ReadWrite

